/*
 * @Author: humengchuan 531537052@qq.com
 * @Date: 2025-03-18 15:26:27
 * @LastEditors: humengchuan 531537052@qq.com
 * @LastEditTime: 2025-03-18 15:42:18
 * @FilePath: \projecte:\学习\react\react-optimize-ts\src\hooks\useAuthentication.ts
 * @Description: 
 */
import { useEffect } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
type UseAuthGuard = () => void;

const useAuthGuard:UseAuthGuard = () => {
    const navigate = useNavigate();
    const location = useLocation();

    useEffect(() => {
        // 检查用户是否已登录的逻辑
        const isLoggedIn = localStorage.getItem('token'); // 示例检查方法

        if (!isLoggedIn) {
            // 如果用户未登录，则重定向到登录页
            navigate('/login', { state: { from: location }, replace: true });
        }
    }, [navigate, location]);
}

export default useAuthGuard;